<template>
  <div style="width: 100%; height: 100vh; background-color: white; overflow: hidden; display: flex; justify-content: center; align-items: center;">

    <div style="width: 400px; background-color: #fff; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); border-radius: 8px; overflow: hidden; transition: all 0.3s ease-in-out;">

      <div style="color: black; font-size: 30px; text-align: center; padding: 30px 0; background-color: #ff5722; color: white;">欢迎注册</div>

      <el-form ref="form" :model="form" :rules="rules" size="normal" style="padding: 20px;">
        <el-form-item prop="username">
          <el-input :prefix-icon="User" v-model="form.username" placeholder="用户名" />
        </el-form-item>

        <el-form-item prop="password">
          <el-input :prefix-icon="Lock" v-model="form.password" show-password placeholder="密码" />
        </el-form-item>

        <el-form-item prop="confirm">
          <el-input :prefix-icon="Lock" v-model="form.confirm" show-password placeholder="确认密码" />
        </el-form-item>

        <el-form-item>
          <el-button style="width: 100%; font-weight: bold; background-color: #ff5722; color: white; border: none;" type="primary" @click="register" :loading="loading">注册</el-button>
        </el-form-item>

      </el-form>

    </div>
  </div>
</template>

<script>
import { User, Lock } from "@element-plus/icons-vue";
import request from "@/utils/request";

export default {
  name: "UserRegister",
  components: {
    User,
    Lock
  },
  data() {
    return {
      form: {},
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        confirm: [
          { required: true, message: '请确认密码', trigger: 'blur' },
        ],
      },
      loading: false
    }
  },
  methods: {
    register() {
      if (this.form.password !== this.form.confirm) {
        this.$message({
          type: "error",
          message: "两次输入的密码不一致，请重新输入"
        })
        return
      }
      this.loading = true;
      this.$refs['form'].validate((valid) => {
        if (valid) {
          request.post("/user/register", this.form).then(res => {
            this.loading = false;
            if (res.code === '0') {
              this.$message({
                type: "success",
                message: "注册成功"
              })
              this.$router.push("/UserLogin"); // 注册成功之后进行页面跳转，跳转到登录页面
            } else {
              this.$message({
                type: "error",
                message: res.msg
              })
            }
          }).catch(() => {
            this.loading = false;
          });
        } else {
          this.loading = false;
        }
      })
    }
  }
}
</script>

<style scoped>
/* 动态效果 */
div {
  transition: all 0.3s ease-in-out;
}

div:hover {
  transform: scale(1.05);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
}

/* 输入框样式 */
.el-input {
  margin-bottom: 10px;
}

/* 按钮样式 */
.el-button {
  transition: background-color 0.3s ease-in-out;
}

.el-button:hover {
  background-color: #e64a19;
}
</style>